<?php


class BasesfCMSfrontActions extends sfActions
{
    public function preExecute()
    {
        sfContext::getInstance()->getConfiguration()->loadHelpers(array('Menu'));
    }

    public function executeMenuFirst(sfWebRequest $request)
    {
        /*@var $menu_item MenuItem*/

        $menu_item = $this->getRoute()->getObject();

        sfConfig::set( 'sf_menu_item' , $menu_item );

        $route_index = "";

        append_title($menu_item->getTitle());
        $culture = $this->getUser()->getCulture();

        if($menu_item->getRoute()){
            $route_index = $menu_item->getRoute();
        }elseif($menu_item->getContentId()){
            //$route_index = "@content_show?id=".$menu_item->getContentId();
            $route_index  = sfConfig::get('app_sfCMSPlugin_routes_content_show');
            $route_index .= "?id=".$menu_item->getContentId();
            //$route_index = "sfCMSfront/showContent?id=".$menu_item->getContentId();

        }elseif($menu_item->getCategoryId()){
            $route_index = sfConfig::get('app_sfCMSPlugin_routes_category_show')."?id=".$menu_item->getCategoryId();

        }

       
        $this->_forward_route($route_index);




    }


    protected function _forward_route($route_index , $menu_item = null){

        $menu_item = $menu_item ? $menu_item : $this->getRoute()->getObject();

        $module = "";
        $action = "";

        $culture = $this->getUser()->getCulture();
        $parse_url = parse_url($route_index);

        if(isset($parse_url['query'])){
            $this->_add_reqeust_paramters($parse_url['query']);
        }

        if(strpos($route_index,'@')===0){
            $routes = sfContext::getInstance()->getRouting()->getRoutes();

            $defaults = $routes[trim($parse_url['path'],'@')]->getDefaults();
            //var_dump($defaults);exit;
            $module = $defaults['module'];
            $action = $defaults['action'];

        }else{
            //var_dump($parse_url['path']);exit;
            $culture_path = sfConfig::get('sf_i18n') ? '/'.$culture : '';
            //die($culture_path);
            $routes = sfContext::getInstance()->getRouting()->parse($culture_path.'/'.$parse_url['path']);
            //echo "<pre>";print_r($routes);exit;
            $module = $routes['module'];
            $action = $routes['action'];
            //var_dump($module);exit;


        }

        $this->forward($module,$action);

    }

    protected function _add_reqeust_paramters($txt){
        /*@var $parm_holder ParameterHolder*/
        $parm_holder = $this->getRequest()->getParameterHolder();

        $txt = trim($txt);
        $txt = trim($txt,'?');
        if(strpos($txt,'&')){
            $arr = explode('&',$txt);
        }else{
            $arr = array($txt);
        }

        foreach ($arr as $s){
            $v = explode('=',$s);
            $parm_holder->add(array($v[0]=>$v[1]));
        }

    }


}
